package com.example.innovate.controller;


/*
  学科竞赛获奖信息操作
  2021.9.13 update
 */

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AchievementCompetitionDO;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.service.AchieveCompetitionService;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.StarPointService;
import com.example.innovate.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;


@RestController
@RequestMapping("/api/competition")
@Api(tags = "学科竞赛接口")
public class AchieveCompetitionController {
    @Autowired
    AchieveCompetitionService achieveCompetitionService;
    @Autowired
    AdminService adminService;
    @Autowired
    StarPointService starPointService;

//    @UserLoginToken
    @GetMapping("/findAllCompetition")
    @ApiOperation("查找所有学科竞赛获奖信息")
    public Result findAllCompetition(@RequestParam("pageNumber") Integer pageNumber,
                                     @RequestParam("pageSize") Integer pageSize,
                                     @RequestParam("className") String className) throws Exception{
        if(className.equals("superAdmin")){
            List<AchievementCompetitionDO> list= achieveCompetitionService.findAll(pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
        else{
            List<AchievementCompetitionDO> list = achieveCompetitionService.findAllByClassName(className,pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
    }

    //@UserLoginToken
    @PostMapping("/upload")
    @ApiOperation("上传")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("id") int id, @RequestParam("className") String className) throws Exception{
        AchievementCompetitionDO achievementCompetitionDO = achieveCompetitionService.findCompetitionById(id);
        String fileName = FileUtil.upload(file);
        String filePath = "/root/file/";
        File file1 = new File(filePath + fileName);
        // 如果文件存在
        String s = "";
        if (file1.exists()) {
            s = "http://42.193.219.136:8080/images/"+fileName;
        }
        else {
            s = "文件上传失败";
        }
        if(achievementCompetitionDO!=null){
            if(achievementCompetitionDO.getComRemark()!=null){
                String deleteName = achievementCompetitionDO.getComRemark();
                filePath = "/root/file/" ;//C:\\file\\
                File filePast = new File(filePath + deleteName);
                if (filePast.isFile() && filePast.exists()) {
                    filePast.delete();
                }
            }
            achievementCompetitionDO.setComRemark(s);
            achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
            return new Result(200,"success","添加成功",achievementCompetitionDO);
        }
        else{
            AchievementCompetitionDO achievementCompetitionDO1 =new AchievementCompetitionDO();
            achievementCompetitionDO1.setComRemark(s);
            achievementCompetitionDO1.setComClassName(className);
            achievementCompetitionDO1.setComIsTemp("true");
            achieveCompetitionService.addCompetition(achievementCompetitionDO1);
            return new Result(200,"success","添加成功",achievementCompetitionDO1);
        }
    }

    //@UserLoginToken
    @PostMapping("/addCompetition")
    @ApiOperation("添加学科竞赛获奖信息")
    public Result addCompetition(@RequestBody AchievementCompetitionDO achievementCompetitionDO,
                                 @RequestParam("userId") int userId) throws Exception{
        AchievementCompetitionDO achievementCompetitionDO1 = achieveCompetitionService.findCompetitionByRemark(achievementCompetitionDO.getComRemark());
        achievementCompetitionDO.setComUserId(userId);
        achievementCompetitionDO.setComIsTemp("true");
        if(achievementCompetitionDO1==null){
            achieveCompetitionService.addCompetition(achievementCompetitionDO);
            return new Result(200,"success","添加成功",achievementCompetitionDO);
        }
        else{
            achievementCompetitionDO.setComId(achievementCompetitionDO1.getComId());
            achievementCompetitionDO.setComRemark(achievementCompetitionDO1.getComRemark());
            achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
            return new Result(200,"success","添加成功",achievementCompetitionDO);
        }
    }


//    @UserLoginToken
    @PutMapping("/updateCompetition")
    @ApiOperation("修改学科竞赛获奖信息")
    public Result updateCompetition(@RequestBody AchievementCompetitionDO achievementCompetitionDO,
                                    @RequestParam("adminUserName") String adminUserName) throws Exception{
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(adminInfoDO!=null){
            if(adminInfoDO.getAdminIsUpdate().equals("false")){
                return new Result(400,"error","没有修改权限",null);
            }
            else{
                achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
                return new Result(200,"success","修改成功",achievementCompetitionDO);
            }
        }
        else{
            achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
            return new Result(200,"success","修改成功",achievementCompetitionDO);

        }
    }

    //@UserLoginToken
    @DeleteMapping("/deleteCompetition")
    @ApiOperation("删除学科竞赛获奖信息")
    public Result deleteCompetition(@RequestParam("id") int id,
                                    @RequestParam("adminUserName") String adminUserName) throws Exception{
        AchievementCompetitionDO achievementCompetitionDO = achieveCompetitionService.findCompetitionById(id);
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(adminInfoDO!=null){
            if(adminInfoDO.getAdminIsUpdate().equals("false")){
                return new Result(400,"error","没有删除权限",null);
            }
            else{
                achieveCompetitionService.deleteCompetition(id);
                starPointService.addPoint();
                return new Result(200,"success","删除成功","id= "+id);
            }
        }
        else{
            achieveCompetitionService.deleteCompetition(id);
            return new Result(200,"success","删除成功","id= "+id);
        }
    }



}
